<?php

/**
 * This class implements a stack 
 * with ordinary functionality
 * 
 * @author Pablo
 * @since 1.0
 */

class blgStack {
	protected $stack;
	
	function __construct(){
		$this->stack= array();				
	}
	
	public function push($data, $unique=FALSE) {
		array_push($this->stack, $data);
		if($unique)
			$this->stack= array_unique($this->stack);
	}
 
	public function pop() {
		return array_pop($this->stack);
	}
	
	public function shift() {
		return array_shift($this->stack);
	}
	
	public function extract_all($reverse= FALSE){
		if($reverse)
			$this->stack= array_reverse($this->stack);
		
		$return= $this->stack;
		$this->stack= array();
		return $return;
	}
	
	public function is_empty(){
		return empty($this->stack);
	}
	
	public function size(){
		return count($this->stack);
	}
	
	public function peek($position){
		if(count($this->stack) >= $position) {
			return $this->stack[$position];
		}
		
		return NULL;
	}
	
	public function __toString(){
		return implode("\n", $this->stack);
	}	
}